﻿// <copyright file="MainForm.cs" company="Nicholas Piasecki"> 
// Copyright (c) 2009 by Nicholas Piasecki All rights reserved. 
// </copyright>

namespace BarcodeSample.UI.WinForms
{
    using System.Threading;
    using System;
    using System.Globalization;
    using System.Windows.Forms;

    /// <summary>
    /// The main form of the application.
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the MainForm class.
        /// </summary>
        /// 
        BarcodeScannerListener listener;
        public MainForm()
        {
//            BarcodeScannerListener listener;

            this.InitializeComponent();

            listener = new BarcodeScannerListener(this);
            listener.BarcodeScanned += this.OnBarcodeScanned;
        }
        
        /// <summary>
        /// Fired when a barcode is scanned.
        /// </summary>
        /// <param name="sender">the sender</param>
        /// <param name="e">the event arguments</param>
        private void OnBarcodeScanned(object sender, EventArgs e)
        {
            BarcodeScannedEventArgs be;

            be = e as BarcodeScannedEventArgs;
            if (be != null)
            {
                string message;

                message = string.Format(
                    CultureInfo.CurrentCulture,
                    "You scanned {0} from device '{1}'.",
                    be.Barcode,
                    be.DeviceInfo.FriendlyName);
                
                System.Diagnostics.Debug.WriteLine(message);
                this.textBox1.Text += be.Barcode;
                MessageBox.Show(message);
            }
        }
    }
}
